import plugin from '../../../lib/plugins/plugin.js'
import { Render } from '../components/index.js'
import { wordle, phrase } from '../model/index.js';
var words = phrase.wordsphrase

export class wordles extends plugin {
    constructor() {
        super({
            name: '[xingchen-plugin] 猜单词',
            dsc: '[xingchen-plugin] 猜单词',
            event: 'message',
            priority: 1,
            rule: [
                {
                    reg: '^#wordle|#猜单词',
                    fnc: 'wordleinitiate'
                },
                {
                    reg: '^[a-z]{5}$',
                    fnc: 'wordleing'
                },
                {
                    reg: '^#wordlerestart|#重猜单词',
                    fnc: 'wordlerestart'
                },
                {
                    reg: '^#wordlerank|#猜单词排行榜',
                    fnc:'wordlerank'
                }

            ]
        })
    }
    async wordleinitiate(e) {
        if (wordle.Gamestate == 1) {
            e.reply('游戏正在进行中...', true)
            return
        } else {
            var word = words[Math.floor(Math.random() * words.length)];
            wordle.guesses = 0;
            wordle.Gamestate = 1;
            wordle.Currentword = word;
            wordle.answer = new Array(30).fill(0)
            console.log(word)
            e.reply('游戏开始，你需要猜一个由 5 个字母组成的单词，你有 6 次机会。\n绿色表示字母和位置都正确，黄色代表字母正确但位置不正确，灰色代表单词中没有这个字母。', true)
            return await Render.render('game/wordle', {
                wordle,
                element: 'elem'
            }, { e, scale: 1.6 })
        }
    }
    async wordleing(e) {
        let data = JSON.parse(await redis.get(`Yunzai:xingchen-plugin:group${e.group_id}_game`)) || {}
        if (!data[e.user_id]) {
            data[e.user_id] = { name: e.sender.card, game24: 0, wordle: 0 };
        }
        if (wordle.Gamestate == 1) {
            var word = wordle.Currentword;
            var guesses = wordle.guesses;
            var msg = e.msg
            let answer = wordle.answer
            let rawimg = wordle.rawimg
            for (let i = 0; i < 5; i++) {
                rawimg.push(msg[i].toUpperCase())
                let db = word.indexOf(msg[i])
                if (db == -1) {
                    answer[(guesses * 5) + i] = 1
                }
                if (db != -1) {
                    if ( word[i] == msg[i]) {
                        answer[(guesses * 5) + i] = 2
                    }else {
                        answer[(guesses * 5) + i] = 3
                    }
                }
            }
            if (guesses < 6) {
                if (msg === word) {
                    guesses++;
                    wordle.Gamestate = 0;
                    if (guesses  <= 3) {
                        data[e.user_id].wordle = data[e.user_id].wordle + 2
                    }
                    if (guesses >= 4) {
                        data[e.user_id].wordle = data[e.user_id].wordle + 1
                    }
                    await redis.set(`Yunzai:xingchen-plugin:group${e.group_id}_game`, JSON.stringify(data))
                    await Render.render('game/wordle', {
                        wordle,
                        element: 'elem'
                    }, { e, scale: 1.6 })
                    return await e.reply(`祝贺！你在 ${guesses} 回合内完成了游戏。`, true);
                }
                if (guesses < 5) {
                    guesses++;
                    wordle.guesses= guesses
                    return await Render.render('game/wordle', {
                        wordle,
                        element: 'elem'
                    }, { e, scale: 1.6 })
                }
            }
            if (guesses === 6) {
                wordle.Gamestate = 0;
                data[e.user_id].wordle = data[e.user_id].wordle - 1
                await redis.set(`Yunzai:xingchen-plugin:group${e.group_id}_game`, JSON.stringify(data))
                Render.render('game/wordle', {
                    wordle,
                    element: 'elem'
                }, { e, scale: 1.6 })
                return await e.reply(`游戏结束，答案为：${word} \n查询词典：\nhttps://cn.bing.com/dict/search?q=${word}\n积分-1`, true);
            }
        }
    }

    async wordlerestart(e) {
        if (wordle.Gamestate == 0) {
            return e.reply('没有正在进行的游戏', true)
        } else {
            var word = words[Math.floor(Math.random() * words.length)];
            wordle.guesses = 0;
            wordle.Gamestate = 1;
            wordle.Currentword = word;
            wordle.answer = new Array(30).fill(0)
            console.log(word)
            e.reply('游戏已重新开始，你需要猜一个由 5 个字母组成的单词，你有 10 次机会，祝你好运。\n请发送#猜【空格】+单词进行答题', true)
            return await Render.render('game/wordle', {
                wordle,
                element: 'elem'
            }, { e, scale: 1.6 })
        }
    }

    async wordlerank(e) {
        let groupdata = JSON.parse(await redis.get(`Yunzai:xingchen-plugin:group${e.group_id}_game`))
        if (!groupdata) {
            return await e.reply('当前群组没有进行过24点游戏', true)
        }
        const arr = Object.entries(groupdata);
        arr.sort((a, b) => b[1].wordle - a[1].wordle);
        let title = `wordle游排行榜`
        let ranklist = [];
        for (let i = 0; i < arr.length; i++) {
            if (arr[i][1].wordle == 0) {
                continue
            }
            let rank = {
                id: arr[i][0],
                name: arr[i][1].name,
                count: arr[i][1].wordle,
            }
            ranklist.push(rank);
        }
        return await Render.render('game/rank', {
            ranklist,
            title,
            element: 'elem'
        }, { e, scale: 1.6 })
    }
}
